package com.foreknow.oop3;

public class Order {
  private String orderId;//订单的编号
  private double total;//总额
  private  OrderItem[] items;//订单项列表

  public Order(String orderId) {
    this.orderId = orderId;
    this.items = new OrderItem[3];
  }

  //定义一个初始化的方法
//  public void init(String orderId){
//    this.orderId = orderId;
//    this.items = new OrderItem[3];
//  }

  public String getOrderId() {
    return orderId;
  }

  public void setOrderId(String orderId) {
    this.orderId = orderId;
  }

  public double getTotal() {
    calTotal();//调用计算总额的方法
    return total;
  }

  public void setTotal(double total) {
    this.total = total;
  }

  public OrderItem[] getItems() {
    return items;
  }

  //将OrderItem对象保存到OrderItem[] items数组中
  public void setItems(OrderItem item,int i) {
    this.items[i] = item;
  }

  //计算总额(单价*数量=总额)
    public void calTotal(){
      double total = 0;
    if(items!=null){
      //对OrderItem数组进行遍历
      for(int i = 0;i<items.length;i++){
        total += items[i].getPprice()*items[i].getNum();
      }
    }
    this.total = total;
  }
}
